package com.adminx.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

/*
 * 公共控制类
 */

public class CommonController extends HttpServlet {
	private static ObjectMapper Mapper = new ObjectMapper() ;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp) ;
	}

	protected <T> T readRequestData(HttpServletRequest req, Class<T> clazz) throws IOException  {
		BufferedReader reader = req.getReader() ;
		StringBuilder builder = new StringBuilder() ;
		String content ;
		while ((content = reader.readLine()) != null) {
			builder.append(content) ;
		}
		if (builder.length() == 0) {
			return null ;
		}
		return Mapper.readValue(builder.toString(), clazz) ;
	}
}
